package com.kdtech.analyse.video;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.ParseState;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
/**
 * 
 * @author axi
 *
 */
public class XiAnCityAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://v.xiancity.cn/(.*/)*?detail_[0-9]{4}_[0-9]{1,2}/[0-9]{1,2}/[0-9]+_[0-9]+.shtml"};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet = true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta video = new NewsMeta();
		String url = urlMeta.getUrl();
		/**
		 * 判断是否为详细页，不是详细页面直接返回空
		 */
		String htmltxt = urlMeta.getHtml();
		if(htmltxt.equals("<script>url = window.location.protocol + \"//\" + window.location.host + \"/\";window.location.href = url;</script>")){
			video.setUpdateUrl(ParseState.ERR404.toString());
			return video;
		}
				
		Document doc = Jsoup.parse(htmltxt);
		
		String title = "";
		String desc = "";
		Long date = null;
		Integer commentNum=0;
		Integer clickNum=0;
		
		/**
		 * 标题
		 */
		Elements selectTitle = doc.select("td.news_title");
		if(selectTitle.size() > 0){
			title = selectTitle.text();
		}
		/**
		 * 时间
		 */
		Elements selectDate = doc.select("td.news_beizhu");
		if(selectDate.size() > 0){
			String matcher = selectDate.toString();
			date = DateUtils.matchDate(matcher);
		}
		/**
		 * 描述
		 */
		desc = doc.select("td.news_text_body").text();
		if("".equals(title)){
		}
		video.setUrl(url);
		video.setTitle(title);
		video.setContent(desc);
		video.setDate(date);
		video.setClickNum(clickNum);
		video.setCommentNum(commentNum);
		video.setType(2);
		return video;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		/**
		 * 不需要更新
		 */
		return null;
	}
	/**
	 * @param args
	 */
	

	
}
